import 'package:boss/color_constant.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/commodity/categories/page/plusCommodityMode/page/plusCommodityByFormField/widget/scan_icon.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';

class TakeoutSearchBar extends StatefulWidget {
  final ValueChanged<String> onChanged;
  final ValueChanged<String> onScan;
  final ValueChanged<bool> onFocusChange;
  final VoidCallback onCancel;
  final bool isShowCancel;

  const TakeoutSearchBar({
    Key key,
    this.onChanged,
    this.onScan,
    this.onFocusChange,
    this.isShowCancel,
    this.onCancel,
  }) : super(key: key);

  @override
  _TakeoutSearchBarState createState() => _TakeoutSearchBarState();
}

class _TakeoutSearchBarState extends State<TakeoutSearchBar> {
  final OutlineInputBorder borderStyle = OutlineInputBorder(
    borderRadius: BorderRadius.all(
      Radius.circular(
        110.w,
      ),
    ),
    borderSide: BorderSide(
      width: 1.px,
      color: ColorConstant.e6e6,
    ),
  );

  final TextEditingController controller = TextEditingController();

  final FocusNode focusNode = FocusNode();

  @override
  void initState() {
    super.initState();

    focusNode.addListener(handleFocus);
  }

  handleFocus() {
    if (focusNode.hasFocus) {
      controller.selection = TextSelection(baseOffset: 0, extentOffset: controller.text.length);
    }
    widget?.onFocusChange(focusNode.hasFocus);
  }

  @override
  void dispose() {
    focusNode.dispose();
    controller.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(
        left: 30.w,
        right: 30.w,
        top: 20.w,
      ),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Stack(
              alignment: Alignment.center,
              children: <Widget>[
                SizedBox(
                  height: 60.w,
                  child: TextFormField(
                    focusNode: focusNode,
                    controller: controller,
                    style: TextStyleConstant().normal_1_28,
                    decoration: InputDecoration(
                      hintText: '搜索订单号/手机号/取货码',
                      hintStyle: TextStyleConstant().normal_3_28,
                      border: borderStyle,
                      enabledBorder: borderStyle,
                      focusedBorder: borderStyle,
                      contentPadding: EdgeInsets.symmetric(
                        vertical: 0,
                        horizontal: 30.w,
                      ),
                    ),
                    onChanged: widget.onChanged,
                  ),
                ),
                Positioned(
                  right: 30.w,
                  child: ScanIcon(
                    onScan: widget.onScan,
                    padding: EdgeInsets.all(
                      15.w,
                    ),
                  ),
                ),
              ],
            ),
          ),
          if (widget.isShowCancel)
            LabelButton(
              title: '取消',
              padding: EdgeInsets.only(
                left: 15.w,
              ),
              onTap: () {
                controller.text = '';
                widget.onCancel();
              },
            ),
        ],
      ),
    );
  }
}
